package com.spring.boot.springBoot.jdk8.lambda;

import com.spring.boot.springBoot.jdk8.Person;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by pactera on 2018/1/22.
 */
public class Collection {


    public static void main(String[] args){

        List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
        Collections.sort(names,(String::compareTo));
        System.out.println(names);

        //collect
        //如果需要以Set来替代List，只需要使用Collectors.toSet()就好了。
        List<Person> persons =
                Arrays.asList(
                        new Person("Max", 18),
                        new Person("Peter", 23),
                        new Person("Pamela", 23),
                        new Person("David", 12));
        List<Person> stringList = persons.stream().filter(person -> person.name.startsWith("P")).collect(Collectors.toList());
        System.out.println(stringList);


        //按照年龄对所有人进行分组
        Map<Integer,List<Person>> integerListMap = persons.stream().collect(Collectors.groupingBy(person -> person.age));
        System.out.println(integerListMap);
//        integerListMap.forEach((age,p) -> System.out.println(p));

        //计算所有人的平均年龄
        Double averageAge = persons
                .stream()
                .collect(Collectors.averagingInt(p -> p.age));

        System.out.println(averageAge);     // 19.0
    }


}
